package com.yyw.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyw.bean.User;
import com.yyw.dao.UserDao;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "测试")
public class TestController {

    @Value(value = "${server.port}")
    private String port;

    @Autowired
    private UserDao userDao;

    @CrossOrigin
    @GetMapping("/test/getUser/{tel}")
    public Result getUserByTel(@PathVariable("tel") String tel) {
        User user = userDao.getUserByTel(tel);

        return ResultFactory.success(user);
    }

    @CrossOrigin
    @GetMapping(value = "/test/connect")
    public String getContString(HttpServletRequest request, HttpSession session) {
        String id = session.getId();
        return "connect success: " + port + "  sessionId: " + id;
    }

    @CrossOrigin
    @ApiOperation(value = "获取Result", notes = "接口描述", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户的唯一标识", required = true),
            @ApiImplicitParam(name = "name", value = "用户名", required = true) })
    @GetMapping(value = "/test/getResult")
    public Result getContString2(@RequestParam("id") Integer id, @RequestParam("name") String name) {
        return ResultFactory.success("提交成功");
    }

    @CrossOrigin
    @PostMapping(value = "/test/post/{tel}")
    public Result testPost(@RequestParam(value = "tel", required = false) String tel) {
        return ResultFactory.success(tel);
    }
}
